//在page页面引入app，同时声明变量，获得所需要的全局变量
const app = getApp();
const path = app.globalData.path;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uhide: 0,
    page: 1,
    start: 0,
    limit: 25,
    issueList: [{}],
    searchModel: {
      "programId": null,
      "vehicleId": null,
      "issueSourceId": null,
      "reportNo": "",
      "endReportDate": null,
      "endPlanFinishDate": null,
      "title": "",
      "issueTypeId": null,
      "customer": null,
      "productTypeName": "",
      "partTypeName": "",
      "responsibleDepartmentId": null,
      "ownerName": "",
      "resolutionStatus": [false, false, false, false, false],
      "progressStatus": [false, false, false, false],
      "lifecycleStatus": [true, true, true, false]  // searchModel 中默认值 不查询“已关闭” 状态的问题
    },

    checkBoxItem: [
      {
        code: '0',
        value: '草稿',
        checked: 'true'
      },
      {
        code: '1',
        value: '待处理',
        checked: 'true'
      },
      {
        code: '2',
        value: '进行中',
        checked: 'true'
      },
      {
        code: '3',
        value: '已关闭'
      }
    ]

  },

  notificationCheck: function() {
    var that = this;

    let loginFlag = wx.getStorageSync('token');

    // 查询未读的Qi问题 通知数量  
    wx.request({
      url: path + 'wx-mw/notify-list!getNotifyListCount.action',
      header: {
        'token': loginFlag
      },
      dataType: "JSON",
      success: function(res) {
        // console.log(res);
        var result = JSON.parse(res.data);
        that.setData({
          unreadNoticeCount: result.data.count
        })
        // console.log(that.data.unreadNoticeCount);
      },
      fail: function(res) {

      }
    })
  },

  checkboxChange: function (e) {

    var that = this;
    // console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    var selectedIndexList = e.detail.value;
    // console.log(selectedIndexList);
    let loginFlag = wx.getStorageSync('token');

    // 每次改变条件重置一下 分页参数  
    that.setData({ 
      page: 1, 
      start: 0 
    })  

    if (loginFlag) {
      // 从searchModel 中拿到 lifecycleStatus
      var lifecycleStatus = [false,false,false,false];
      for(var i=0; i< selectedIndexList.length; i++){
        lifecycleStatus[selectedIndexList[i]] = true;
      }
      // console.log(lifecycleStatus);
      // 给初始的searchModel 中的lifecycleStatus 赋值
      that.data.searchModel.lifecycleStatus = lifecycleStatus;
      // console.log(that.data.searchModel);
      wx.showLoading({ title: '查询中...' });
      wx.request({
        url: path + "wx-issue/q-iissue!getQIIssue.action",

        header: {
          'token': loginFlag
        },
        dataType: "JSON",
        data: {
          searchModel: that.data.searchModel,
          page: 1,
          start: 0,
          limit: 25
        },
        success: function (res) {
          var result = JSON.parse(res.data);

          that.setData({
            issueList: result.data

          })
          wx.hideLoading();
        },
        error: function (res) {
          // console.log(res);
        }
      })

    } else {

      // 无skey，作为首次登录
      wx.navigateTo({ url: '../../../login/login' });
    }

  },

  
  inputSearch:function(e){
    this.setData({
      inputValue: e.detail.value
    })        

  },

  doSearch: function() {

    var that = this;
    
    let loginFlag = wx.getStorageSync('token');
    // console.log(that.data.inputValue);
    
    that.data.searchModel.title = that.data.inputValue;
    if (loginFlag) {
 
      wx.showLoading({ title: '查询中...' });
      wx.request({
        url: path + "wx-issue/q-iissue!getQIIssue.action",

        header: {
          'token': loginFlag
        },
        dataType: "JSON",
        data: {
          searchModel: that.data.searchModel,
          page: 1,
          start: 0,
          limit: 25
        },
        success: function (res) {
          var result = JSON.parse(res.data);

          that.setData({
            issueList: result.data

          })
          wx.hideLoading();
        },
        error: function (res) {
          // console.log(res);
        }
      })

    } else {

      // 无skey，作为首次登录
      wx.navigateTo({ url: '../../../login/login' });
    }
  },

  jumpToNotification: function() {
    var that = this;
    wx.navigateTo({ url: '../../notification/notification' });
  },

  jumpToView: function(e) {
    var index = parseInt(e.currentTarget.dataset.index);
    var dataset = e.currentTarget.dataset;
    var data = {};
    data.issueId = dataset.issueid;
    data.processstatus = dataset.processstatus;
    data.lifecyclestatus = dataset.lifecyclestatus;
    data.programvehicleid = dataset.programvehicleid;
    // console.log(dataset);

    if (dataset.lifecyclestatus == 'DRAFT') { // 草稿要跳转到创建的页面
      wx.navigateTo({
        url: '../issueList/issueDraft?issueId=' + dataset.issueid +
        '&lifecycleStatus=' + dataset.lifecyclestatus +
        '&programVehicleId=' + dataset.programvehicleid,
      });
      return;
    }
    wx.navigateTo({
      url: '../issueList/issueForm?issueId=' + dataset.issueid +
        '&processStatus=' + dataset.processstatus +
        '&lifecycleStatus=' + dataset.lifecyclestatus +
        '&programVehicleId=' + dataset.programvehicleid,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    

    var that = this;

    let loginFlag = wx.getStorageSync('token');
    that.notificationCheck(); 
    
    if (loginFlag) {
      wx.request({
        url: path + "wx-issue/q-iissue!getQIIssue.action",

        header: {
          'token': loginFlag
        },
        dataType: "JSON",
        data: {
         searchModel:that.data.searchModel,
          page: 1,
          start: 0,
          limit: 25
        },
        success: function(res) {
          var result = JSON.parse(res.data);

          that.setData({
            issueList: result.data
          })
          // console.log(that.data.issueList);
        },
        error: function(res) {
          // console.log(res);
        }
      })

    } else {

      // 无skey，作为首次登录
      wx.navigateTo({ url: '../../login/login' });
    }
  },

  //切换隐藏和显示 
  toggleBtn: function(event) {
    var that = this;
    var toggleBtnVal = that.data.uhide;
    // console.log(event)

    var itemId = event.currentTarget.id;
    if (toggleBtnVal == itemId) {
      that.setData({
        uhide: 0
      })
    } else {
      that.setData({
        uhide: itemId
      })
    }

    that.notificationCheck();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    wx.showNavigationBarLoading()
    that.onShow();
    setTimeout(() => {
      this.getData = '数据拿到了'
      wx.stopPullDownRefresh()
      wx.hideNavigationBarLoading()
    }, 3000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

    var that = this;
    // 显示加载图标

    var loginFlag = wx.getStorageSync('token');
    wx.showLoading({
      title: '玩命加载中',
    })
    // 页数+1
    //page = page + 1;
    that.setData({
      page: that.data.page + 1,
      start: that.data.start + 25,
    })

    wx.request({
      url: path + "wx-issue/q-iissue!getQIIssue.action",
      header: {
        'token': loginFlag
      },
      dataType: "JSON",
      data: {
        searchModel: that.data.searchModel,
        page: that.data.page,
        start: that.data.start,
        limit: 25
      },
      success: function(res) {
        var issues = that.data.issueList;
        var result = JSON.parse(res.data);
        // console.log("-------------");
        // console.log(result);
        var len = result.data.length;
        if (len == 0) {
          wx.hideLoading();
          wx.showToast({
            title: '没有数据了',
            duration: 1500,
            icon: 'none'
          })
          return;
        }
        // console.log(len);
        for (var i = 0; i < len; i++) {
          issues.push(result.data[i]);
        }
        // 设置数据
        that.setData({
          issueList: issues
        })
        // 隐藏加载框
        wx.hideLoading();

      },
      error: function(res) {
        // console.log(res);
      }
    })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})